package com.hand.wfive.activity

import android.content.Context
import android.graphics.Color
import androidx.core.content.ContextCompat
import com.hand.wfive.R
import com.hand.wfive.ad.AdActivity
import com.hand.wfive.adapter.FragmentAdapter
import com.hand.wfive.base.BaseFragment
import com.hand.wfive.fragment.HomeFragment
import com.hand.wfive.fragment.SignLibraryFragment
import com.hand.wfive.fragment.TestFragment
import com.hand.wfive.loginAndVip.UserManager
import com.hand.wfive.loginAndVip.ui.LoginIndexActivity
import com.hand.wfive.loginAndVip.ui.MineFragment
import com.hand.wfive.util.CheckVersionUtils
import com.hand.wfive.util.FuncSwitchUtils
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.tab.QMUITab
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.startActivity
import java.util.ArrayList

/**
 * 首页
 */
class MainActivity : AdActivity() {

    override fun getContentViewId() = R.layout.activity_main

    override fun init() {
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
        initTabs()
        initPages()
        UserManager.getInstance().initUser()
        FuncSwitchUtils.getSwitchValue()
        CheckVersionUtils.checkAppVersion()
              /*CheckVersionUtils.checkAppVersion() {
                  if (it != ApiConfig.SUCCESSCODE) {
                      //其它请求活动弹窗相关，挪到此回调里处理，防止重复弹窗
                  }
              }*/
    }

    companion object {
        @JvmStatic
        fun show(context: Context) {
            if (UserManager.getInstance().isLogin) {
                context.startActivity<MainActivity>()
            } else {
                LoginIndexActivity.show(context)
            }
        }
    }

    private fun initTabs() {
        pager.setSwipeable(false)
        val textSize = QMUIDisplayHelper.sp2px(this, 10)
        val builder = tabs.tabBuilder()
        builder.setSelectedIconScale(1.0f).setTextSize(textSize, textSize)
            .setColor(Color.parseColor("#dbdbdb"), Color.parseColor("#2C5CCB"))
            .setDynamicChangeIconColor(false).skinChangeWithTintColor(false)
        tabs.addTab(getTab(builder, R.mipmap.tab1_nor, R.mipmap.tab1_sel, "首页"))
        tabs.addTab(getTab(builder, R.mipmap.tab2_nor, R.mipmap.tab2_sel, "签字库"))
        tabs.addTab(getTab(builder, R.mipmap.tab3_nor, R.mipmap.tab3_sel, "我的"))
        tabs.notifyDataChanged()
    }

    private fun getTab(
        builder: QMUITabBuilder, normalDrawableId: Int, selectedDrawableId: Int, title: String
    ): QMUITab {
        return builder.setNormalDrawable(ContextCompat.getDrawable(this, normalDrawableId))
            .setNormalIconSizeInfo(
                QMUIDisplayHelper.dp2px(this, 22),
                QMUIDisplayHelper.dp2px(this, 22)
            )
            .setSelectedDrawable(ContextCompat.getDrawable(this, selectedDrawableId))
            .setText(title).build(this)
    }

    private fun initPages() {
        val pages = ArrayList<BaseFragment>()
        pages.add(HomeFragment())
        pages.add(SignLibraryFragment())
        pages.add(MineFragment())
        pager.adapter = FragmentAdapter(supportFragmentManager, pages)
        pager.offscreenPageLimit = pages.size
        tabs.setupWithViewPager(pager, false)
    }

    override fun onResume() {
        super.onResume()
        if (UserManager.getInstance().isVip && bannerView.childCount > 0) {
            bannerView.removeAllViews()
        }
    }
}